$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/users/list',
        datatype: "json",
        colModel: [
			{ label: '用户名', name: 'username', index: 'username', width: 80 }, 			
			{ label: '手机号', name: 'telphone', index: 'telphone', width: 80 },
            { label: '头像', name: 'headimg', index: 'headimg', width: 80 , formatter:function (value, options, row) {
                    return '<img  style="width: 50px" src="' + value + '">';
                }},
			{ label: '余额', name: 'balance', index: 'balance', width: 80 },
			{ label: '性别', name: 'gender', index: 'gender', width: 80 },
			{ label: '注册时间', name: 'createtime', index: 'createtime', width: 80 },
			{ label: '登录方式', name: 'type', index: 'type', width: 80, formatter: function(value, options, row){
                    return value === 0 ?
                        '微信' :
                        '支付宝';
                } }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});
layui.use('upload', function(){
    var upload = layui.upload;
});
layui.use('element', function(){
    var element = layui.element;
});
var vm = new Vue({
	el:'#rrapp',
	data:{
        q: {
            username: null,
            telphone:null
        },
		showList: true,
		title: null,
		users: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.users = {};

            $("#xzImg").show()
            var upload = layui.upload; //得到 upload 对象
            console.log(upload);
            upload.render({
                elem: '#xzImg'
                ,url: baseURL + 'sys/users/uploadImg/'
                ,auto: false //选择文件后不自动上传
                ,field : 'file'
                ,bindAction: '#isImg' //指向一个按钮触发上传
                ,choose: function(obj){
                    var files = obj.pushFile();
                    obj.preview(function(index, file, result){
                        $("#xsImg").html('<img src="'+ result +'" width="500px"   alt="'+ file.name +'">')
                    });
                }
                ,before: function(obj){ //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
                    layer.load(); //上传loading
                }
                ,done: function(res, index, upload){ //上传后的回调
                    if(res.code==0){
                        layer.closeAll()
                        layer.msg("上传成功")
                        $("#img").val(res.fileName);
                    }
                }
            })

		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.users.id == null ? "sys/users/save" : "sys/users/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.users),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "sys/users/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getInfo: function(id){
			$.get(baseURL + "sys/users/info/"+id, function(r){
                vm.users = r.users;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData: {'username': vm.q.username,"telphone":vm.q.telphone
                },
                page:page
            }).trigger("reloadGrid");
		}
	}
});